package com.lanou.myim.ui.activity;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMGroupManager;
import com.easemob.chat.EMMessage;
import com.easemob.exceptions.EaseMobException;
import com.lanou.myim.R;

/**
 * Created by dllo on 16/3/1.
 */

public class LogInActivity extends AppCompatActivity {
    private ProgressDialog dialog;
    private EditText etUserName;
    private EditText etPassword;
    private Button loading;
    private Button register;
    private String userName;
    private String password;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if (message.arg1==1){
                dialog.dismiss();
                Toast.makeText(LogInActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(LogInActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EMChat.getInstance().isLoggedIn()) {
            startActivity(new Intent(LogInActivity.this, MainActivity.class));
            finish();
        }
        setContentView(R.layout.activity_login);
        loading = (Button) findViewById(R.id.btn_loading);
        register = (Button) findViewById(R.id.btn_register);
        etUserName = (EditText) findViewById(R.id.et_userName);
        etPassword = (EditText) findViewById(R.id.et_password);
        etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        etUserName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                etPassword.setText(null);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
//        try {
//            if (EMChatManager.getInstance().getContactUserNames() != null) {
//
//                etUserName.setText(EMChatManager.getInstance().getContactUserNames().toString());
//            }
//        } catch (EaseMobException e) {2 q q      7  7  5
//            e.printStackTrace();a a 9
//        }

        loading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog = ProgressDialog.show(LogInActivity.this,"正在登陆","请稍后");
                try {
                    if (EMChatManager.getInstance().getContactUserNames() != null) {
                        EMChatManager.getInstance().logout();
                    }
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }

                userName = String.valueOf(etUserName.getText());
                password = String.valueOf(etPassword.getText());
                if (userName.length()>1&&password.length()>1) {
                    EMChatManager.getInstance().login(userName, password, new EMCallBack() {
                                @Override
                                public void onSuccess() {
                                    runOnUiThread(new Runnable() {
                                        public void run() {
                                            EMGroupManager.getInstance().loadAllGroups();
                                            EMChatManager.getInstance().loadAllConversations();
                                            dialog.dismiss();
                                            handler.sendEmptyMessage(0);
                                            Log.d("main", "登陆聊天服务器成功!");
                                            finish();
                                        }
                                    });
                                    //    Toast.makeText(LogInActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(LogInActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }

                                @Override
                                public void onError(int i, String s) {
                                    dialog.dismiss();
                                    Message msg = new Message();
                                    msg.arg1 = 1;
                                    handler.sendMessage(msg);
                                    Log.d("LogInActivity", "i   :" + i);
                                    Log.d("LogInActivity", "545" + s);
                                }

                                @Override
                                public void onProgress(int i, String s) {
                                    Log.d("LogInActivity", "i:" + i);
                                    Log.d("LogInActivity", s);
                                }
                            }

                    );
                }
                else {
                    dialog.dismiss();
                    Toast.makeText(LogInActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LogInActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });


    }

}
